package com.founder.bdyx.modules.sys.service.impl;

import com.founder.bdyx.core.pdfUtil.CreateTemplate;
import com.founder.bdyx.core.util.DateUtil;
import com.founder.bdyx.core.util.DictConvertUtil;
import com.founder.bdyx.modules.sys.mapper.ApplyInfoMapper;
import com.founder.bdyx.modules.sys.model.ApplyInfo;
import com.founder.bdyx.modules.sys.model.SysUser;
import com.founder.bdyx.modules.sys.service.SendService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("sendService")
@Transactional
public class SendServiceImpl implements SendService {

    @Autowired
    ApplyInfoMapper applyInfoMapper;
    @Value("${pdf_save_path}")
    private String PDF_SAVE_PATH;

    /**
     * 生成执行单
     * @param id 订单id
     * @return
     * @throws Exception
     */
    @Override
    public File execOrder(Integer id,HttpServletRequest request) throws Exception {
        ApplyInfo applyInfo = applyInfoMapper.getById(id);
        SysUser user = (SysUser) request.getSession().getAttribute("user");
        ApplyInfo convertEntity = new ApplyInfo();
        BeanUtils.copyProperties(convertEntity,applyInfo);
        DictConvertUtil.converter(convertEntity);
        String path = PDF_SAVE_PATH + applyInfo.getUuid() + ".pdf";
        File file = new File(path);
        if(!file.exists()){
            JSONArray array = new JSONArray();
            JSONObject mMapDatas = new JSONObject();
            mMapDatas.put("uuid", convertEntity.getUuid());
            mMapDatas.put("isPay", convertEntity.getPayStatus());
            mMapDatas.put("transType", convertEntity.getTransType());
            mMapDatas.put("printName", user.getRealName());
            mMapDatas.put("printTime", DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm"));
            mMapDatas.put("idcName", convertEntity.getIdCard().getIdcName());
            mMapDatas.put("startTime", DateUtil.formatDate(convertEntity.getStartTime(),"yyyy-MM-dd HH:mm"));
            mMapDatas.put("endTime", DateUtil.formatDate(convertEntity.getEndTime(),"yyyy-MM-dd HH:mm"));
            mMapDatas.put("idcNo", convertEntity.getIdCard().getIdcNo());
            mMapDatas.put("depName", convertEntity.getDepName());
            mMapDatas.put("cureNo", convertEntity.getCureNo());
            mMapDatas.put("purpContent", convertEntity.getPurpContent());
            mMapDatas.put("printCount", convertEntity.getPrintCount());
            mMapDatas.put("singlePrice", convertEntity.getSinglePrice());
            mMapDatas.put("transPrice", convertEntity.getTransPrice());
            mMapDatas.put("payPrice", convertEntity.getPayPrice());
            mMapDatas.put("recvName", convertEntity.getRecInfo().getRecvName());
            mMapDatas.put("recvMobile", convertEntity.getRecInfo().getRecvMobile());
            String pname = convertEntity.getRecInfo().getPcaInfo().getPname();
            String cname = convertEntity.getRecInfo().getPcaInfo().getCname();
            String aname = convertEntity.getRecInfo().getPcaInfo().getAname();
            String address = convertEntity.getRecInfo().getRecvAddr();
            mMapDatas.put("recvAddr", pname +" "+ cname +" "+ aname +" "+ address);
            array.add(mMapDatas);

            String tPdfTemplateFile = PDF_SAVE_PATH + "execOrderTemplate.pdf";// 获取模板路径

            CreateTemplate.pdfout(array,tPdfTemplateFile,file);
            applyInfo.setExecOrderNum(applyInfo.getExecOrderNum() + 1);
            int num = applyInfoMapper.updateExecOrderNum(applyInfo);
        }
        return file;
    }

    /**
     * 批量生成执行单
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public List<File> batchExecOrder(Integer[] ids, HttpServletRequest request) throws Exception {
        List<File> files = new ArrayList<>();
        for (int i = 0; i<ids.length; i++){
            File file = execOrder(ids[i],request);
            files.add(file);
        }
        return files;
    }
}
